package com.six.address.controller;

import com.six.address.service.ReceiveService;
import com.six.core.entity.Receive;
import com.six.core.util.JwtUtil;
import com.six.core.util.WebUtil;
import com.six.core.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author: WangPeng
 * @createTime: 2023/03/02 10:58
 * @description:
 * @version: v1.0
 */
@Api(value = "/address/address", tags = {":"})
@RestController
@RequestMapping("/address/address")
public class ReceiveController {

    @Autowired
    private ReceiveService receiveService;

    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    @PostMapping("/findAllByUserId")
    public Result findAllByUserId(){
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> map = JwtUtil.decodeJWT(token);
        Integer tbUserid = (Integer) map.get("tbUserid");
        return receiveService.findAllReceive(tbUserid);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Receive", name = "receive", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    @PostMapping("/saveReceive")
    public Result saveReceive(@RequestBody Receive receive){
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> map = JwtUtil.decodeJWT(token);
        Integer tbUserid = (Integer) map.get("tbUserid");
        receive.setUserid(tbUserid);

        return receiveService.savaReceive(receive);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "receiveId", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    @PostMapping("/deleteReceive")
    public Result deleteReceive(Integer receiveId){
        return receiveService.deleteReceive(receiveId);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Receive", name = "receive", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    @PostMapping("/updateReceive")
    public Result updateReceive(@RequestBody Receive receive){
        return receiveService.updateReceive(receive);
    }
}
